package com.mcxtzhang.indexlib.IndexBar.helper;

import com.mcxtzhang.indexlib.suspension.ISuspensionInterface;

import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

/**
 * 介绍：IndexBar 的 数据相关帮助类 实现
 * * 1 将汉语转成拼音(利用tinyPinyin)
 * 2 填充indexTag (取拼音首字母)
 * 3 排序源数据源
 * 4 根据排序后的源数据源->indexBar的数据源
 * 作者：zhangxutong
 * 邮箱：mcxtzhang@163.com
 * 主页：http://blog.csdn.net/zxt0601
 * 时间： 2016/11/28.
 */

public class IndexBarDataHelperImpl implements IIndexBarDataHelper
{
    @Override
    public IIndexBarDataHelper sortSourceDatas(List<? extends ISuspensionInterface> datas)
    {
        if (null == datas || datas.isEmpty())
        {
            return this;
        }
        //对数据源进行排序
        Collections.sort(datas, new Comparator<ISuspensionInterface>()
        {
            @Override
            public int compare(ISuspensionInterface lhs, ISuspensionInterface rhs)
            {
                if (null == lhs || null == rhs) return 0;
                // 两者均排序
                if (lhs.fixAlign() == ISuspensionInterface.SORT && rhs.fixAlign() == ISuspensionInterface.SORT)
                    return lhs.sortContent().compareTo(rhs.sortContent());

                // 两者固定方式相同，不改变顺序
                if (lhs.fixAlign() == rhs.fixAlign()) return 0;
                // 两者不同，说明 rhs为 footer
                if (lhs.fixAlign() == ISuspensionInterface.HEADER)
                    return -1;
                // 两者不同，说明 rhs为 header
                if (lhs.fixAlign() == ISuspensionInterface.FOOTER)
                    return 1;

                return 0;
            }
        });
        return this;
    }

    @Override
    public IIndexBarDataHelper getSortedIndexDatas(List<? extends ISuspensionInterface> sourceDatas, List<String> indexDatas)
    {
        if (null == sourceDatas || sourceDatas.isEmpty())
        {
            return this;
        }
        //按数据源来 此时sourceDatas 已经有序
        int size = sourceDatas.size();
        String baseIndexTag;
        Set<String> indexSet = new LinkedHashSet();
        Set<String> headerSet = null;
        Set<String> footerSet = null;
        for (ISuspensionInterface i : sourceDatas)
        {
            if (null == i.getIndexTag() || "".equals(i.getIndexTag()))
                continue;

            switch (i.fixAlign())
            {
                case ISuspensionInterface.HEADER:
                    if (null == headerSet)
                        headerSet = new LinkedHashSet<>();
                    headerSet.add(i.getIndexTag().toUpperCase());
                    break;
                case ISuspensionInterface.FOOTER:
                    if (null == footerSet)
                        footerSet = new LinkedHashSet<>();
                    footerSet.add(i.getIndexTag().toUpperCase());
                    break;
                default:
                    indexSet.add(i.getIndexTag().toUpperCase());
                    break;
            }
        }
        if (null != headerSet && !headerSet.isEmpty())
        {
            indexDatas.addAll(headerSet);
            headerSet.clear();
            headerSet = null;
        }
        indexDatas.addAll(indexSet);
        if (null != footerSet && !footerSet.isEmpty())
        {
            indexDatas.addAll(footerSet);
            footerSet.clear();
            footerSet = null;
        }
        indexSet.clear();
        indexSet = null;
        return this;
    }
}
